package com.zyy.smartcity1129.movie;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.zxing.WriterException;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.data.MovieFilmDetail;
import com.zyy.smartcity1129.data.MovieRoomInfo;
import com.zyy.smartcity1129.data.MovieSeatsInfo;
import com.zyy.smartcity1129.data.MovieTheatres;
import com.zyy.smartcity1129.data.MovieTicketInfo;
import com.zyy.smartcity1129.util.PpUtil;
import com.zyy.smartcity1129.util.QRCode;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: MovieTicketFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 29/11/2021$
 * @Version: 1.0
 */
public class MovieTicketFragment extends BaseFragment {
    ExecutorService executor = Executors.newFixedThreadPool(1);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    private RecyclerView rvMovieOrder;
    List<MovieTicketInfo.RowsBean> ticketList = new ArrayList<>();
    RvAd<MovieTicketInfo.RowsBean> rvAd;
    String movieName;
    String movieTheatreName;
    String movieRoomName;
    String movieRow;
    String movieCol;
    String code;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_order;
    }

    @Override
    protected String title() {
        return "影票";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                ticketList = Http.getInstance().get("/prod-api/api/movie/ticket/list", null, MovieTicketInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 1) {
                initAdapter(ticketList);
                rvMovieOrder.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMovieOrder.setAdapter(rvAd);
            }
        });
    }

    private void initAdapter(List<MovieTicketInfo.RowsBean> mTicketList) {
        rvAd = new RvAd<MovieTicketInfo.RowsBean>(mTicketList, R.layout.rv_order) {
            private TextView tvRvOrder1, tvRvOrder2, tvRvOrder3, tvRvOrder4, tvRvOrder5, tvRvOrder6,
                    tvRvOrder7, tvRvOrder8, tvRvOrder9, tvRvOrder10;

            private void initView(View v) {
                tvRvOrder1 = (TextView) v.findViewById(R.id.tvRvOrder1);
                tvRvOrder2 = (TextView) v.findViewById(R.id.tvRvOrder2);
                tvRvOrder3 = (TextView) v.findViewById(R.id.tvRvOrder3);
                tvRvOrder4 = (TextView) v.findViewById(R.id.tvRvOrder4);
                tvRvOrder5 = (TextView) v.findViewById(R.id.tvRvOrder5);
                tvRvOrder6 = (TextView) v.findViewById(R.id.tvRvOrder6);
                tvRvOrder7 = (TextView) v.findViewById(R.id.tvRvOrder7);
                tvRvOrder9 = (TextView) v.findViewById(R.id.tvRvOrder9);
                tvRvOrder8 = (TextView) v.findViewById(R.id.tvRvOrder8);
                tvRvOrder10 = (TextView) v.findViewById(R.id.tvRvOrder10);
            }

            @Override
            protected void initR(View iv, MovieTicketInfo.RowsBean rowsBean, int p) {
                initView(iv);
                tvRvOrder3.setVisibility(View.GONE);
                tvRvOrder4.setVisibility(View.GONE);
                tvRvOrder6.setVisibility(View.GONE);
                tvRvOrder9.setVisibility(View.GONE);
                tvRvOrder10.setText("购买时间：" + rowsBean.getBuyDate());
                code = rowsBean.getCode();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        movieName = Http.getInstance().get("/prod-api/api/movie/film/detail/" + rowsBean.getMovieId(), null, MovieFilmDetail.class, 1).getData().getName();
                        movieTheatreName = Http.getInstance().get("/prod-api/api/movie/theatre/" + rowsBean.getTheaterId(), null, MovieTheatres.class, 1).getData().getName();
                        movieRoomName = Http.getInstance().get("/prod-api/api/movie/theatre/room/" + rowsBean.getRoomId(), null, MovieRoomInfo.class, 1).getData().getName();
                        movieRow = Http.getInstance().get("/prod-api/api/movie/theatre/seat/" + rowsBean.getSeatId(), null, MovieSeatsInfo.class, 1).getData().getRow();
                        movieCol = Http.getInstance().get("/prod-api/api/movie/theatre/seat/" + rowsBean.getSeatId(), null, MovieSeatsInfo.class, 1).getData().getCol();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvRvOrder1.setText(movieName);
                                tvRvOrder2.setText(movieTheatreName);
                                tvRvOrder5.setText(movieRoomName);
                                tvRvOrder8.setText(movieRow + "排" + movieCol + "号");
                            }
                        });
                    }
                }).start();
                switch (rowsBean.getStatus()) {
                    case "0":
                        tvRvOrder7.setText("过期");
                        break;
                    case "2":
                        tvRvOrder7.setText("已核销");
                        break;
                    case "1":
                        tvRvOrder7.setText("有效");
                        break;
                }
//                二维码
                iv.setOnClickListener(new View.OnClickListener() {
                    private void initView(View view) {
                        imgPQR = (ImageView) view.findViewById(R.id.imgPQR);
                        framePQR = (FrameLayout) view.findViewById(R.id.framePQR);
                    }

                    private ImageView imgPQR;
                    private FrameLayout framePQR;

                    @Override
                    public void onClick(View v) {
                        if (tvRvOrder7.getText().toString().equals("有效")) {
                            PpUtil PpUtil = new PpUtil();
                            View view = PpUtil.getP(requireContext(), R.layout.p_qr);
                            initView(view);
                            QRCode qrCode = new QRCode();
                            framePQR.setOnClickListener(v1 -> {
                                PpUtil.closeP();
                            });
                            try {
                                Bitmap bitmap = qrCode.qrCode(code);
                                imgPQR.setImageBitmap(bitmap);
                            } catch (WriterException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        };
    }

    private void initView(View v) {
        rvMovieOrder = (RecyclerView) v.findViewById(R.id.rvMovieOrder);
    }
}
